package com.winona.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.taplinker.client.R;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;
import com.taplinker.core.util.TimeUtil;
import com.taplinker.core.util.json.JsonUtil;
import com.winona.Base1Activity;
import com.winona.ConstantValue;
import com.winona.bean.Message;
import com.winona.widget.PullToRefreshView;

public class ImportantNewsActivity extends Base1Activity {
	private int PAGE_SIZE = 10;
	private TextView title;
	private ImageButton back;
	private Intent intent;
	private PullToRefreshView mRefreshView;
	private ListView mListView;

	private BaseAdapter mAdapter;
	private List<Message> messageList;
	private boolean hasMore = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inport_news);
		findView();
		init();
		setLinstener();
		getMessageList();
	}

	private void init() {
		title.setText("重要消息");
		messageList = new ArrayList<Message>();
		mRefreshView.disableScroolDown();
		mRefreshView.enableScroolUp();
		mRefreshView
				.setOnFooterRefreshListener(new PullToRefreshView.OnFooterRefreshListener() {

					@Override
					public void onFooterRefresh(PullToRefreshView view) {
						getMessageList();
					}
				});
	}

	private void getMessageList() {
		if (!hasMore) {
			Toast.makeText(getApplicationContext(), "没有了", Toast.LENGTH_SHORT)
					.show();
			mRefreshView.onFooterRefreshComplete();
			return;
		}
		new SimpleBaseCommand() {

			@Override
			public Result go() {
				int gotopage = 1;
				if (messageList != null && messageList.size() > PAGE_SIZE) {
					gotopage = messageList.size() / PAGE_SIZE;
					if (messageList.size() % PAGE_SIZE > 0) {
						gotopage += 1;
					}
				}
				return new HttpTemplate().exchange(ConstantValue.COMMON
						+ "message/finds?gotopage=" + gotopage + "&pagesize="
						+ PAGE_SIZE);
			}

			@Override
			public void yes(Result result) {
				mRefreshView.onFooterRefreshComplete();
				String json = result.object.toString();
				if (StringUtils.isNotEmpty(json)) {
					try {
						org.json.JSONObject jo = new org.json.JSONObject(json);
						String jstr = null;
						jstr = jo.getString("body");
						List<Message> list = JsonUtil.jsonToList(jstr,
								Message.class);
						if (list != null && list.size() < PAGE_SIZE) {
							hasMore = false;
						}
						messageList.addAll(list);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if (mAdapter == null) {
					mAdapter = new MessagesAdapter();
					mListView.setAdapter(mAdapter);
				} else {
					mAdapter.notifyDataSetChanged();
				}
			}

			@Override
			public void no(Result result) {
				mRefreshView.onFooterRefreshComplete();
			}

		}.execute();
	}

	private void setLinstener() {
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				startActivity(PerToolActivity.class);
			}
		});

	}

	private void findView() {
		title = (TextView) findViewById(R.id.inport_title);
		back = (ImageButton) findViewById(R.id.inport_back);
		mRefreshView = (PullToRefreshView) findViewById(R.id.pull_refresh_view);
		mListView = (ListView) findViewById(R.id.inport_list_view);
	}

	private final class MessagesAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return messageList.size();
		}

		@Override
		public Object getItem(int position) {
			return messageList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder vHolder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(ImportantNewsActivity.this)
						.inflate(R.layout.messge_list_item, null);
				vHolder = new ViewHolder();
				vHolder.time = (TextView) convertView.findViewById(R.id.time);
				vHolder.content = (TextView) convertView
						.findViewById(R.id.content);
				convertView.setTag(vHolder);
			} else {
				vHolder = (ViewHolder) convertView.getTag();
			}

			final Message message = (Message) getItem(position);

			vHolder.time.setText(TimeUtil.timeFormat(message.getAddtime(),
					"MM月dd日 HH:mm:ss"));
			vHolder.content.setText(message.getContent());

			return convertView;
		}

		private final class ViewHolder {
			TextView time;
			TextView content;

		}

	}
}
